﻿using System.Collections.Generic;
using System.Windows;
using VNet.VisiSuite.Common.Logger;
using VNet.VisiSuite.VisiBoard.Dal.EntityClasses;
using VNet.VisiSuite.VisiBoard.Models;

namespace VNet.VisiSuite.VisiBoard.ViewModels
{
    public class ChangeoverDialogViewModel : DialogViewModel
    {
        #region Fields
        private ChangeoverDialogModel _model;
        private Visibility _visibility;
        private dynamic _selected_production_plan;
        private string _error;
        private OperatorEntity _operator = null;

        #endregion

        public ChangeoverDialogViewModel()
        {
            _visibility = Visibility.Visible;
            Heading = "Changeover";
            Model = new ChangeoverDialogModel();
        }

        #region Properties

        public dynamic SelectedProductionPlan
        {
            get { return _selected_production_plan; }
            set
            {
                _selected_production_plan = value;
                OnPropertyChanged("SKU");
                OnPropertyChanged("OperationPass");
                OnPropertyChanged("Planned");
                OnPropertyChanged("Actual");
            }
        }

        public string OperatorCode { get; set; }

        public string SKU
        {
            get { return _selected_production_plan != null ? _selected_production_plan.Sku : "";  }
        }

        public string OperationPass
        {
            get { return _selected_production_plan != null ? _selected_production_plan.OperationPass.ToString() : ""; }
        }

        public string Planned
        {
            get { return _selected_production_plan != null ? _selected_production_plan.Planned.ToString("#,##0") : ""; }
        }

        public string Actual
        {
            get { return _selected_production_plan != null ? _selected_production_plan.Actual.ToString("#,##0") : ""; }
        }

        public string ValidationError
        {
            get { return _error; }
            set
            {
                _error = value;
                OnPropertyChanged("ValidationError");
            }
        }

        public IEnumerable<dynamic> ProductionPlanData
        {
            get
            {
                return _model.ProductionPlanData;
            }
        }

        public OperatorEntity Operator
        {
            get { return _operator; }
        }

        public ChangeoverDialogModel Model
        {
            get { return _model; }
            set
            {
                _model = value;
                OnPropertyChanged("Model");
            }
        }


        public Visibility Visibility
        {
            get { return _visibility; }
            set
            {
                _visibility = value;
                OnPropertyChanged("Visibility");
            }
        }

        public bool validate()
        {
            bool success = false;
            string err_msg;
            int production_plan_id = _selected_production_plan != null ? _selected_production_plan.ProductionPlanId : 0;

            if (production_plan_id <= 0)
            {
                err_msg = "Please select a job #.";
                success = false;
            }
            else
            {
                success = _model.validateOperator(OperatorCode, production_plan_id, out err_msg, out _operator);
            }

            ValidationError = err_msg;
            return success;
        }

        #endregion


    }
}
